//
//  CoverTableViewController.m
//  79环信
//
//  Created by 刘恒槟 on 15/7/31.
//  Copyright (c) 2015年 刘恒槟. All rights reserved.
//

#import "CoverTableViewController.h"
#import "EaseMob.h"
#import "UIViewController+HUD.h"
#import "UITableViewCell+Func.h"

@interface CoverTableViewController ()


@property (strong, nonatomic)NSMutableArray * cover_arr;

@end

@implementation CoverTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self getConverList];
    
}

//-(void)viewWillDisappear:(BOOL)animated{
//    
//    [super viewWillDisappear:animated];
//    
//    self.tabBarController.hidesBottomBarWhenPushed = YES;
//}

-(void)getConverList
{
    self.cover_arr = [NSMutableArray array];

    
    [self.cover_arr addObjectsFromArray:[[EaseMob sharedInstance].chatManager loadAllConversationsFromDatabaseWithAppend2Chat:YES]];
    
    [[EaseMob sharedInstance].chatManager asyncFetchMyGroupsListWithCompletion:^(NSArray *groups, EMError *error) {
        if (!error) {
            
            
            [self.cover_arr addObjectsFromArray:groups];
            
//            [self.tableView reloadData];
        }
    } onQueue:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.cover_arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"coverCell" forIndexPath:indexPath];
    NSDictionary *dic = self.cover_arr[indexPath.row];
    
    [cell setCellInfoWithNSDictionary:dic];
    
//    EMConversation * conver = self.cover_arr[indexPath.row];
    
    
//     [self.groupName setTitle:conver.chatter forState:0];
    NSLog(@"=========%@",self.cover_arr);
   
    
    return cell;
}



// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    
    return YES;
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return @"删除";
    
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    EMConversation * conver = self.cover_arr[indexPath.row];
    
    BOOL delete = [[EaseMob sharedInstance].chatManager removeConversationByChatter:conver.chatter deleteMessages:YES append2Chat:NO];
    
    if (delete)
    {
        
        [self getConverList];
        
    }
    else
    {
        
        [self showHint:@"删除失败"];
    }
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    EMConversation * conversantion = self.cover_arr[indexPath.row];
    
    UIStoryboard * story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UIViewController * vc = [story instantiateViewControllerWithIdentifier:@"chat"];
    
    
    [vc setValue:conversantion forKey:@"conversation"];
    
    [self.navigationController pushViewController:vc animated:YES];
    
    
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
